package com.imc.platform.base.role.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.imc.core.utils.MysqlUtil;
import com.imc.platform.base.role.dao.IRoleMapper;
import com.imc.platform.base.role.pojo.Role;
import com.imc.platform.base.role.service.IRoleService;
import com.imc.platform.base.user.dao.mapper.IUserMapper;
import com.imc.platform.base.user.pojo.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 *类描述：角色管理service实现
 *开发者：QY
 *创建时间：2018/8/17 15:44
 **/
@Slf4j
@Service
@Transactional
public class RoleServiceImpl implements IRoleService {


    @Autowired
    private IRoleMapper roleMapper;

    @Autowired
    private IUserMapper userMapper;

    @Autowired
    MysqlUtil mysqlUtil;

    /**
     *QY 2018/8/17 15:44
     *方法描述：查询所有角色详情
     **/
    @Override
    public PageInfo<Role> queryRoleList(int pageNum, int pageSize) {
        //调取公用分页方法
        PageHelper.startPage(pageNum,pageSize,true);
        PageInfo info= new PageInfo<>(roleMapper.queryRoleList());
        return info;
    }

    @Override
    public int updateState(String state,String roleId) {
        if("false".equals(state)){
            state="2";
        }else if("true".equals(state)){
            state="1";
        }
        return roleMapper.updateState(state,roleId);
    }

    @Override
    public PageInfo<User> queryUserList(int pageNum, int pageSize,String roleId) {
        List<String>userList=roleMapper.queryUserIdByRoleId(roleId);
        PageHelper.startPage(pageNum,pageSize,true);
        PageInfo info= new PageInfo<>(userMapper.queryUserListByUserId(userList));
        return info;
    }

    @Override
    public PageInfo<User> queryUserListByRoleId(int pageNum, int pageSize, String roleId) {
        PageHelper.startPage(pageNum,pageSize,true);
        PageInfo info= new PageInfo<>(userMapper.queryUserListByRoleId(roleId));
        return info;
    }

    @Override
    public int insertRoleMember(List<String> memberList,String roleId) {
        Map<String,Object> resMap=new HashMap<String,Object>();
        resMap.put("roleId",roleId);
        resMap.put("memberList",memberList);
        roleMapper.delRoleUserByMemberList(memberList);
        int count= roleMapper.insertRoleMember(resMap);
        if(count>0){
            String duty="";
            switch (roleId){
                case "1":
                    duty="master";
                case "2":
                    duty="branch";
                case "3":
                    duty="teacher";
                case "4":
                    duty="student";
            }
            resMap.put("duty",duty);
            userMapper.updateUserDuty(resMap);
        }
        return count;
    }

    @Override
    public int delRoleMember(List<String> memberList, String roleId) {
        Map<String,Object> resMap=new HashMap<String,Object>();
        resMap.put("roleId",roleId);
        resMap.put("memberList",memberList);
        return roleMapper.delRoleMember(resMap);
    }

    @Override
    public int delRole(String roleId) {
        //删除角色与用户关联表的数据
        roleMapper.delRoleUserByRoleId(roleId);
        roleMapper.delRoleMenuByRoleId(roleId);
        return roleMapper.delRole(roleId);
    }

    @Override
    public int delRoleUserById(String roleId, String userId) {
        return roleMapper.delRoleUserById(roleId,userId);
    }

    @Override
    public PageInfo<User> queryUserByUserName(int pageNum, int pageSize,String selectParms,String orgName,String duty) {
        PageHelper.startPage(pageNum,pageSize,true);
        PageInfo info= new PageInfo<>(userMapper.queryUserByUserName(selectParms,orgName,duty));
        return info;
    }

    @Override
    public PageInfo<User> queryUserBySelectUserName(int pageNum, int pageSize, String selectParms,String roleId) {
        PageHelper.startPage(pageNum,pageSize,true);
        PageInfo info= new PageInfo<>(userMapper.queryUserBySelectUserName(selectParms,roleId));
        return info;
    }

    @Override
    public int queryRoleNameByText(String roleName) {
        List<Role>retList=roleMapper.queryRoleNameByText(roleName);
        if(retList.size()>0){
            return 1;
        }
        return 0;
    }

    @Override
    public int addRole(Map<String, Object> parms) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
//        parms.put("id",mysqlUtil.getMaxId("t_role"));
        parms.put("createTime",df.format(new Date()));
        return roleMapper.addRole(parms);
    }

    @Override
    public int addRoleMenu(List<Map<String, Object>> parmsList, String roleId) {
        List<String>parmList=new ArrayList<>();
        for (Map<String,Object> map:parmsList) {
            for (String s:map.keySet()) {
                if(s.equals("id")){
                    parmList.add(map.get(s).toString());
                }
            }
        }
        Map<String,Object> resMap=new HashMap<String,Object>();
        resMap.put("roleId",roleId);
        resMap.put("parmList",parmList);
        //先根据角色删除关联再添加关联
        int count= roleMapper.delRoleMenuByRoleId(roleId);
        int n= roleMapper.addRoleMenu(resMap);
        return n;
    }

    @Override
    public String queryRoleIdByUserId(String userId) {
        List<String>retList=roleMapper.queryRoleIdByUserId(userId);
        return Collections.min(retList);
    }

}
